package org.itboys.mobile.service.mongo;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductBrandMapper;
import org.itboys.mobile.dao.product.SundayProductCategoryMapper;
import org.itboys.mobile.entity.mongo.SundayProductCategoryBrand;
import org.itboys.mobile.entity.mysql.product.SundayProductBrand;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年4月26日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代__商品_分类关联的品牌_service。如果属性不够，请自行填充
 */
@Service
public class SundayProductCategoryBrandService extends BaseMongoService<SundayProductCategoryBrand>{

    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayProductCategoryBrand> getEntityClass() {
        return SundayProductCategoryBrand.class;
    }

    @Autowired
    private ResourceHolder resourceHolder;
    @Autowired
    private SundayProductBrandMapper brandMapper;
    @Autowired
    private SundayProductCategoryMapper categoryMapper;
    /**
     * 保存分类的品牌
     * @param categoryId
     * @param brandIds
     */
    public void save(Long categoryId,List<Long> brandIds){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("categoryId",categoryId);
        super.doDelete(param);
        List<SundayProductCategoryBrand> categoryBrands= new ArrayList<>();
        for(Long brandId:brandIds){
            categoryBrands.add(new SundayProductCategoryBrand(categoryId,brandId));
        }
        if(ListUtil.isNotNull(categoryBrands)){
            super.batchSaveWithoutLogin(categoryBrands);
        }
    }

    /**
     * 获取分类的关联的品牌树
     * @param categoryId
     */
    public List<Map<String,Object>> getTree(Long categoryId){
        Map<String,Object> param = new HashMap<String,Object>();
        List<SundayProductBrand> brands = brandMapper.select(param);
        List<Long> brandIds = getByCategoryId(0l,categoryId);
        List<Map<String,Object>> results = new ArrayList<>();
        for(SundayProductBrand brand:brands){
            boolean checked=false;
            Map<String,Object> result = new HashMap<>();
            for(Long brandId:brandIds){
                if(brand.getId().longValue()==brandId.longValue()){
                    checked=true;
                    break;
                }
            }
            result.put("id",brand.getId());
            result.put("name",brand.getName());
            result.put("checked",checked);
            results.add(result);
        }
        return results;
    }

    /**
     * 通过分类ID获取匹配ID
     * @param firstCatId
     * @param secondCatId
     */
    public List<Long> getByCategoryId(Long firstCatId,Long secondCatId){
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        if(LongUtil.isNotZreo(firstCatId)){
            List<Long> secondCatIds = categoryMapper.findByParent(firstCatId);
            if(ListUtil.isNotNull(secondCatIds)){
                param.put("categoryId in",secondCatIds);
            }
        }
        if(LongUtil.isNotZreo(secondCatId)){
            param.put("categoryId",secondCatId);
        }
        List<Long> brandIds = new ArrayList<>();
        brandIds.add(0l);
        List<SundayProductCategoryBrand> categoryBrands = super.list(param);
        for(SundayProductCategoryBrand categoryBrand:categoryBrands){
            brandIds.add(categoryBrand.getBrandId());
        }

        return brandIds;

    }

}
